﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Business.AllocateManagement.Dto;
using Business.BorrowManagement.Dto;
using Business.Models;
using Business.ProductCuringManagement.Dto;
using Microsoft.EntityFrameworkCore;
using RazorEngine.Compilation.ImpromptuInterface.InvokeExt;
using SixLabors.ImageSharp;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;
using XCZ;
using Yitter.IdGenerator;

namespace Business.AllocateManagement
{
    public class AllocateAppService : BusinessWorkFlowAppService,IAllocateAppService
    {
        private IRepository<Allocate, Guid> _repository;
        private IRepository<AllocationDetails, Guid> _repository1;
        public AllocateAppService(IRepository<Allocate, Guid> repository, IRepository<AllocationDetails, Guid> repository1)
        {
            _repository = repository;
            _repository1 = repository1;
        }

        /// <summary>
        /// 新增或修改
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<AllocateDto> CreateOrUpdate(AllocateDto input)
        {

            Allocate result = null;
            if (!input.Id.HasValue)
            {
                input.Id = GuidGenerator.Create();
                input.DocumentNum = YitIdHelper.NextId().ToString();
                Allocate entity = new Allocate()
                {
                    DocumentSubject=input.DocumentSubject,
                    DocumentNum=input.DocumentNum,
                    Auditors = input.Auditors,
                    Applicants = input.Applicants,
                    ApplicationTime = input.ApplicationTime,
                    AssociateProject = input.AssociateProject,
                    AllocationRemarks=input.AllocationRemarks
                };

                foreach (var item in input.createOrUpdateAllocateDto)
                {
                    AllocationDetails details = new AllocationDetails()
                    {
                        AllocateId=input.Id,
                        Number=item.Number,
                        ProductName=item.ProductName,
                        ProductNumber = item.ProductNumber,
                        SKU = item.SKU,
                        Unit = item.Unit,
                        Applications = item.Applications,
                        Desc = item.Desc,   
                    };
                    await _repository1.InsertAsync(details);
                }
                result = await _repository.InsertAsync(ObjectMapper.Map<AllocateDto, Allocate>(input));
            }
            else
            {
                var data = await _repository.GetAsync(input.Id.Value);
                result = await _repository.UpdateAsync(ObjectMapper.Map(input, data));
                //检查流程状态
                //await WorkFlow.CheckFormStatus(input.Id.Value, result);
            }

            return ObjectMapper.Map<Allocate, AllocateDto>(result);
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<AllocateDto>> GetAllocateList(AllocateInputDto input)
        {
            var query = (from b in await _repository.GetQueryableAsync()

                             // on b.TenantId equals d.BorrowId
                         select new AllocateDto
                         {
                             Id = b.Id,
                             DocumentSubject = b.DocumentSubject,
                             DocumentNum = b.DocumentNum,
                             Auditors = b.Auditors,
                             Applicants = b.Applicants,
                             ApplicationTime = b.ApplicationTime,
                             AssociateProject = b.AssociateProject,
                             AllocationRemarks = b.AllocationRemarks,
                             createOrUpdateAllocateDto = (from d in _repository1.GetQueryableAsync().Result
                                              where b.Id == d.AllocateId
                                              select new CreateOrUpdateAllocateDto
                                              {
                                                  Number = d.Number,
                                                  ProductName = d.ProductName,
                                                  ProductNumber = d.ProductNumber,
                                                  SKU = d.SKU,
                                                  Unit = d.Unit,
                                                  Applications = d.Applications,
                                                  Desc = d.Desc
                                              }
                             ).ToList()
                         }
                       ).ToList();

            var totalCount = query.Count();
            var items = query.Skip(input.SkipCount - 1).Take(input.MaxResultCount).ToList();

            //var dtos = ObjectMapper.Map<List<BorrowM>, List<BorrowDto>>(items);
            return new PagedResultDto<AllocateDto>(totalCount, query);
        }

        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<AllocateDto> GetAllocateForEdit(Guid id)
        {
            var data = await _repository.GetAsync(id);
            var dto = ObjectMapper.Map<Allocate, AllocateDto>(data);
            return dto;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task DeleteAllocate(List<Guid> ids)
        {
            foreach (var item in ids)
            {
                await _repository.DeleteAsync(item);
            }
        }
    }
}
